<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Replace property
 * 
 *  Replace a property for all objects in this ESCAPE instance.
 */
class escape_setup_action_ReplaceProperty extends escape_setup_Action
{
	public function handleRequest()
	{
		$viewName = 'PropertyReplaceSelect';
		
		// fetch POST parameters
		$sourceProperty = $_POST['sourceProperty'];
		$targetProperty = $_POST['targetProperty'];
		$rdfType = $_POST['rdfType'];
		$includeSubTypes = $_POST['includeSubTypes'] == '1';
		$dryRun = $_POST['dryRun'] == '1';
		
		// initially set include sub types and dry run to true
		if($sourceProperty == '' && $targetProperty == '' && $rdfType == '')
		{
			$includeSubTypes = true;
			$dryRun = true;
		}
		
		$result = '';

		$repository = new escape_repository_Repository($this->requestAttributes['repositoryConfig']);
		$repository->initialize();
		
		$repositorySchema = $repository->repositorySchema;

		// check if the source and target properties are provided
		if($sourceProperty != '' && $targetProperty != '')
		{
			try
			{
				// replace the property
				$modifiedObjectUris = $repository->replaceProperty($sourceProperty, $targetProperty, $rdfType, $includeSubTypes, $dryRun);
				
				$result .= "Number of objects modified: " . count($modifiedObjectUris) . "\n";
				$result .= "\n";
				$result .= "Modified objects:\n";
				$result .= implode("\n", $modifiedObjectUris);
				$result .= "\n";
			}
			catch(Exception $exception)
			{
				$result .= "An '" . get_class($exception) . "' exception occurred\n";
				$result .= "Exception details: " . $exception->getCode() . " - " . $exception->getMessage() . "\n";
			}		
		
			$result .= 'Done';
			
			$this->requestAttributes['result'] = $result;
			
			$viewName = 'Result';
		}

		$repository->shutdown();
		
		// set the request attributes
		$this->requestAttributes['sourceProperty'] = $sourceProperty;
		$this->requestAttributes['targetProperty'] = $targetProperty;
		$this->requestAttributes['rdfType'] = $rdfType;
		$this->requestAttributes['includeSubTypes'] = $includeSubTypes;
		$this->requestAttributes['dryRun'] = $dryRun;
		
		return $viewName;
	}
}